<?php
require_once "db.php";
session_start();

function time_date($s_time, $c_date, $s_date){
	return (strtotime($s_time)-strtotime($c_date))+strtotime($s_date);
}

$content = "<div id='list-reservations'>";
$accountid = $_SESSION['uid'];
$current_date = date("Y/m/d");
$current_time = date("H:i:s");
$list_reservations_query_text = "SELECT movies_idmovies, projection_idprojection, COUNT(projection_idprojection) AS tickets FROM reservations WHERE accounts_idaccounts = '$accountid' GROUP BY projection_idprojection";
$list_reservations_query = db_query($list_reservations_query_text);
while ($row = mysqli_fetch_assoc($list_reservations_query)) {
	$content .= "<div class='my-reservations-row'>";
	$projid = $row['projection_idprojection'];
	$proj_data_query_text = "SELECT proj_date, proj_begin, cinema_idcinema FROM projection WHERE idprojection = '$projid'";
	$proj_data_query = db_query($proj_data_query_text);
	$row_proj = mysqli_fetch_assoc($proj_data_query);

	$movieid = $row['movies_idmovies'];
	$movie_name_text = "SELECT movie_name FROM movies WHERE idmovies='$movieid'";
	$movie_name = db_query($movie_name_text);
	$row_movie = mysqli_fetch_assoc($movie_name);
	
	if (time_date($row_proj['proj_begin'], $current_date, $row_proj['proj_date'])<strtotime($current_time)) {
		$content .= "<div class='stat-movie-name' style = 'background-color: #131558'><a href='#'>".$row_movie['movie_name']."</a></div>";
	}
	else{
		$content .= "<div class='stat-movie-name' style = 'background-color: #10812d'><a href='#'>".$row_movie['movie_name']."</a></div>";
	}
	$cinemaid = $row_proj['cinema_idcinema'];
	$cinema_name_text = "SELECT cinema_name FROM cinema WHERE idcinema='$cinemaid'";
	$cinema_name = db_query($cinema_name_text);
	$row_cinema = mysqli_fetch_assoc($cinema_name);
	$content .= "<div class='stat-cinema-name'>".$row_cinema['cinema_name']."</div><div class='stat-proj-date'>".$row_proj['proj_date'].
				"</div><div class='stat-proj-begin'>".$row_proj['proj_begin']."</div><div class='stat-proj-tickets'>".$row['tickets']." ticket(s)</div>";
	$check_rating_query_text = "SELECT personal_rating FROM reservations WHERE projection_idprojection='$projid' 
								AND accounts_idaccounts='$accountid' LIMIT 1";
	$check_rating_query = db_query($check_rating_query_text);
	$row_rating = mysqli_fetch_assoc($movie_name);	
	if ($row_rating['personal_rating']== 0) {
		$content .= "<div class='rate'><a href='#'>Rate now</a></div>";
	}
	else{
		$content .= "<div class='my-rate'>".$row_rating['personal_rating']."</div>";
	}
	$content .= "<div class='my-comment' data-movieid='".$movieid."'><a href='#'>My comment</a></div>
	</div>";
}

$content .= '</div>';
echo $content;
